package com.lottery.controller.qiuqiu.messageController;

import com.alibaba.fastjson.JSONObject;
import com.lottery.controller.BaseController;
import com.lottery.dto.ResultDto;
import com.lottery.service.qiuqiu.MsgService.MsgNoticeService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/api/msg")
public class MsgController extends BaseController {
    @Resource
    private MsgNoticeService msgNoticeService;

    /**
     * 拉取公屏消息
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryMessage", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto queryMessage(HttpServletRequest request){
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer userId = obj.getInteger("userId");
            String agencyMrak = obj.getString("agencyMark");
            Integer index = obj.getInteger("index");
            Integer lotteyType = obj.getInteger("lotteyType");
            result.setData2(lotteyType);
            msgNoticeService.queryMessage(userId,agencyMrak,index,lotteyType,result);
        } catch (Exception e) {
            logger.error("MsgController cancelWithdrawById error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }

    /**
     * 插入聊天记录
     * @param request
     * @return
     */
    @RequestMapping(value = "/insertBarrageforUser", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto insertBarrageforUser(HttpServletRequest request){
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer agencyMark = obj.getInteger("agencyMark");
            Integer userId = obj.getInteger("userId");
            String msg = obj.getString("msg");
            msgNoticeService.insertBarrageforUser(agencyMark,userId,msg,result);
        } catch (Exception e) {
            logger.error("MsgController cancelWithdrawById error,{}", e);
            result.setResultData(false, "插入聊天记录失败", 1, null);
        }
        return result;
    }


    /**
     * 拉取客服的回复消息
     * @param request
     * @return
     */
    @RequestMapping(value = "/getMsgInfoFromCusServer", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto getMsgInfoFromCusServer(HttpServletRequest request){
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer userId = obj.getInteger("userId");
            Integer agencyMark = obj.getInteger("agencyMark");
            msgNoticeService.getMsgInfoFromCusServer(userId,agencyMark,result);
        } catch (Exception e) {
            logger.error("MsgController getMsgInfoFromCusServer error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }

}
